package com.block.datapush.service.apipush.utils;

import cn.hutool.core.util.StrUtil;
import com.block.common.enums.BaseTypeInfoEnum;
import org.apache.commons.lang3.StringUtils;

import javax.crypto.Cipher;
import java.nio.charset.StandardCharsets;
import java.security.KeyFactory;
import java.security.PublicKey;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

/**
 * 乐乐花 双授权
 */
public class LeLeHuaUtil {

    //性别
    public static Integer getSex(Integer sex) {
        if (sex == 1) {
            return 1;
        }if (sex == 2) {
            return 2;
        }
        return 0;
    }

    //IP
    public static String getReqIp(String ip){
        if (StringUtils.isNotEmpty(ip)){
            return ip;
        }
        return "127.0.0.1";
    }

    //芝麻分
    public static Integer getZhimaScore(String zhima) {
        if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm001.getValue(), zhima)) {
            return 41;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm002.getValue(), zhima)) {
            return 42;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm003.getValue(), zhima)) {
            return 43;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Zhima.zm004.getValue(), zhima)) {
            return 44;
        }
        return 45;
    }








    //贷款额度
    public static String getloan(String loan) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la003.getValue(), loan) || StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la004.getValue(), loan) || StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la005.getValue(), loan)) {
            return "5万以上";
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanAmount.la001.getValue(), loan)) {
            return "1-3万";
        }
        return "3-5万";

    }

    //公积金
    public static String getGjj(String gongjijin) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj001.getValue(), gongjijin)) {
            return "无";
        }if (StrUtil.equals(BaseTypeInfoEnum.Gjj.gjj002.getValue(), gongjijin)) {
            return "6个月以下";
        }
        return "6个月以上";
    }


    //社保
    public static String getSocialInsurance(String shebao) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shebao.sb001.getValue(), shebao)) {
            return "无";
        }if (StrUtil.equals(BaseTypeInfoEnum.Shebao.sb002.getValue(), shebao)) {
            return "6个月以下";
        }
        return "6个月以上";
    }



    //信用情况
    public static String getyuqi(String yuqi) {
        if (StrUtil.equals(BaseTypeInfoEnum.Yuqi.yq001.getValue(), yuqi)) {
            return "无逾期";
        }else if (StrUtil.equals(BaseTypeInfoEnum.Yuqi.yq002.getValue(), yuqi)){
            return "三个月内有逾期";
        }
        return "一年内有逾期";
    }

    //投保情况
    public static String getbaodan(String baodan) {
        if (StrUtil.equals(BaseTypeInfoEnum.Baoxian.bx001.getValue(), baodan)) {
            return "无";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Baoxian.bx002.getValue(), baodan)) {
            return "一年以下";
        }
        return "一年以上";
    }

    //职业
    public static String getjob(String job) {
        if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz001.getValue(), job)) {
            return "上班族";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz002.getValue(), job)) {
            return "企业主";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Gongzuo.gz003.getValue(), job)) {
            return "无固定职业";
        }
        return "公务员/国企";
    }

    //房产情况
    public static String getHouse(String house) {
        if (StrUtil.equals(BaseTypeInfoEnum.House.hu001.getValue(), house)) {
            return "无房产";
        } else if (StrUtil.equals(BaseTypeInfoEnum.House.hu003.getValue(), house)) {
            return "有房可抵押";
        }else if (StrUtil.equals(BaseTypeInfoEnum.House.hu004.getValue(), house)) {
            return "有房不抵押";
        }
        return "未知";
    }

    //车产情况
    public static String getCar(String car) {
        if (StrUtil.equals(BaseTypeInfoEnum.Car.car001.getValue(), car)) {
            return "无车产";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Car.car002.getValue(), car)) {
            return "有车可抵押";
        } else if (StrUtil.equals(BaseTypeInfoEnum.Car.car003.getValue(), car)) {
            return "有车不抵押";
        }
        return "未知";
    }



    public static Integer getjdbaitiao(String jdbaitiao) {
        if (StrUtil.equals(BaseTypeInfoEnum.Baitiao.bt001.getValue(), jdbaitiao)) {
            return 0;
        }
        return 2;
    }
    public static Integer gethuabei(String buabei) {
        if (StrUtil.equals(BaseTypeInfoEnum.Huabei.hb001.getValue(), buabei)) {
            return 0;
        }
        return 2;
    }




    public static Integer  getxueli(String xueli) {
         if (StrUtil.equals(BaseTypeInfoEnum.XueLi.xl003.getValue(), xueli)) {
            return 4;
        } else if (StrUtil.equals(BaseTypeInfoEnum.XueLi.xl004.getValue(), xueli)) {
            return 5;
        } else if (StrUtil.equals(BaseTypeInfoEnum.XueLi.xl005.getValue(), xueli)) {
            return 6;
        }
        return 3;
    }







    public static Integer getloantime(String loantime) {
        if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt001.getValue(), loantime)) {
            return 0;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt002.getValue(), loantime)) {
            return 1;
        } else if (StrUtil.equals(BaseTypeInfoEnum.LoanTime.lt003.getValue(), loantime)) {
            return 2;
        }
        return 3;

    }

    public static Integer getYongtu(String yongtu) {
        if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt001.getValue(), yongtu)) {
            return 0;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt004.getValue(), yongtu)) {
            return 2;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt002.getValue(), yongtu)) {
            return 4;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Yongtu.yt003.getValue(), yongtu)) {
            return 3;
        }
        return 0;
    }
    public static Integer getShouRu(String shouru) {
        if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr001.getValue(), shouru)) {
            return 0;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr002.getValue(), shouru)) {
            return 1;
        } else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr003.getValue(), shouru)) {
            return 3;
        }else if (StrUtil.equals(BaseTypeInfoEnum.Shouru.sr004.getValue(), shouru)) {
            return 4;
        }
        return 2;
    }


    /**
     *
     * @param content 待加密的明文字符串
     * @param publicKey RSA公钥
     * @return 密文数据
     * @throws Exception
     */
    public static String encrypt(String content, String publicKey) {
        try {
            byte[] keyBytes= Base64.getDecoder().decode(publicKey.getBytes());
            X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
            KeyFactory keyFactory = KeyFactory.getInstance("RSA");
            PublicKey key = keyFactory.generatePublic(keySpec);
            Cipher cipher = Cipher.getInstance("RSA");
            cipher.init(Cipher.ENCRYPT_MODE, key);
            byte[] result = cipher.doFinal(content.getBytes(StandardCharsets.UTF_8));
            return Base64.getEncoder().encodeToString(result);
        } catch(Exception e) {
            e.printStackTrace();
            return null;
        }
    }

}
